home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / toolkit / vbof_v11 / demoaddr.cls next >
Text File  |  1996-03-02  |  17KB  |  610 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "Address"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = True
  8. Option Explicit
  9.  
  10. ' the following pertain to being supported by
  11. '   VBOFCollection, VBOFObjectManager and
  12. '   VBOFEventManager
  13. Public ObjectID As Long
  14. Public ObjectChanged As Long
  15. Public ObjectAdded As Long
  16. Public ObjectDeleted As Long
  17. Public ObjectParentCount As Long
  18. Public ObjectManager As VBOFObjectManager
  19.  
  20. ' the following code pertains to the business
  21. '   of the Address object
  22. Private pvtLine1 As String
  23. Private pvtLine2 As String
  24. Private pvtLine3 As String
  25. Private pvtCity As String
  26. Private pvtStateCode As String
  27. Private pvtState As State
  28. Private pvtStateObjectID As Long
  29. Private pvtZipCode As Long
  30. Private pvtZipSupplement As Integer
  31. Private pvtZipExtension As Integer
  32. Private pvtForeignZipCode As String
  33. Private pvtStatus As String
  34. Private pvtUsage As String
  35. Public StateCodes As VBOFCollection
  36.  
  37. Public Function ObjectDBGridUnboundAddData(Optional DBGrid As Variant, Optional RowBuf As Variant, Optional NewRowBookmark As Variant) As Boolean
  38. ' Populate the object variables with the values
  39. '   provided by the user in the new row of the
  40. '   DBGrid
  41. '   (in support of VBOFCollection)
  42. '
  43. ' Parameter Description:
  44. '   DBGrid:= the DBGrid which is being
  45. '       populated
  46. '   RowBuf:= the current DBGrid RowBuf object
  47. '   NewRowBookmark:= the row number being processed
  48.  
  49.     Dim I As Long
  50.     
  51.     For I = 0 To RowBuf.ColumnCount - 1
  52.         If Not IsNull(RowBuf.Value(0, I)) Then
  53.             Select Case RowBuf.ColumnName(I)
  54.                 Case "Line1"
  55.                     Line1 = RowBuf.Value(0, I) & ""
  56.                 Case "Line2"
  57.                     Line2 = RowBuf.Value(0, I) & ""
  58.                 Case "Line3"
  59.                     Line3 = RowBuf.Value(0, I) & ""
  60.                 Case "City"
  61.                     City = RowBuf.Value(0, I) & ""
  62.                 Case "StateCode"
  63.                     StateCode = RowBuf.Value(0, I) & ""
  64.                 Case "ZipCode"
  65.                     ZipCode = CLng("0" & RowBuf.Value(0, I))
  66.                 Case "ZipSupplement"
  67.                     ZipSupplement = CLng("0" & RowBuf.Value(0, I))
  68.                 Case "ZipExtension"
  69.                     ZipExtension = CLng("0" & RowBuf.Value(0, I))
  70.                 Case "Status"
  71.                     Status = RowBuf.Value(0, I) & ""
  72.                 Case "Usage"
  73.                     Usage = RowBuf.Value(0, I) & ""
  74.     
  75. ' Note:  Do not initialize the ObjectID.
  76.         
  77.             End Select
  78.         End If
  79.     Next I
  80.     
  81. ' return "OK" status
  82.     ObjectDBGridUnboundAddData = True
  83. End Function
  84.  
  85. Public Function ObjectDBGridUnboundReadData(Optional DBGrid As Variant, Optional RowBuf As Variant, Optional RowNumber As Variant) As Boolean
  86. ' Populate the DBGrid RowBuf with values from
  87. '   variables within this object
  88. '   (in support of VBOFCollection)
  89. '
  90. ' Parameter Description:
  91. '   DBGrid:= the DBGrid which is being
  92. '       populated
  93. '   RowBuf:= the current DBGrid RowBuf object
  94. '   RowNumber:= the row number being processed
  95.  
  96.     Dim I As Long
  97.     
  98.     For I = 0 To RowBuf.ColumnCount - 1
  99.         Select Case RowBuf.ColumnName(I)
  100.             Case "Line1"
  101.                 RowBuf.Value(RowNumber, I) = Line1
  102.             Case "Line2"
  103.                 RowBuf.Value(RowNumber, I) = Line2
  104.             Case "Line3"
  105.                 RowBuf.Value(RowNumber, I) = Line3
  106.             Case "City"
  107.                 RowBuf.Value(RowNumber, I) = City
  108.             Case "StateCode"
  109.                 RowBuf.Value(RowNumber, I) = StateCode
  110.             Case "ZipCode"
  111.                 RowBuf.Value(RowNumber, I) = ZipCode
  112.             Case "ZipSupplement"
  113.                 RowBuf.Value(RowNumber, I) = ZipSupplement
  114.             Case "ZipExtension"
  115.                 RowBuf.Value(RowNumber, I) = ZipExtension
  116.             Case "Status"
  117.                 RowBuf.Value(RowNumber, I) = Status
  118.             Case "Usage"
  119.                 RowBuf.Value(RowNumber, I) = Usage
  120.             Case "FormattedZip"
  121.                 RowBuf.Value(RowNumber, I) = FormattedZip
  122.             Case "ForeignZipCode"
  123.                 RowBuf.Value(RowNumber, I) = ForeignZipCode
  124.             Case "ObjectID"
  125.                 RowBuf.Value(RowNumber, I) = ObjectID
  126.         End Select
  127.     Next I
  128. End Function
  129. Public Function ObjectDataSource() As String
  130. ' Return the Data Source with which this Class is associated
  131. '   (in support of VBOFCollection)
  132.     
  133.     ObjectDataSource = "Addresses"
  134. End Function
  135.  
  136. Public Function FormattedAddress() As String
  137. ' Return a displayable, fully formatted
  138. '   version of Me
  139.  
  140.     Dim ReturnString As String
  141.     
  142.     If Line1 > "" Then
  143.         ReturnString = _
  144.             pvtContatenateWithCRLF( _
  145.                 String1:=ReturnString, _
  146.                 String2:=Line1)
  147.     End If
  148.     
  149.     If Line2 > "" Then
  150.         ReturnString = _
  151.             pvtContatenateWithCRLF( _
  152.                 String1:=ReturnString, _
  153.                 String2:=Line2)
  154.     End If
  155.     
  156.     If Line3 > "" Then
  157.         ReturnString = _
  158.             pvtContatenateWithCRLF( _
  159.                 String1:=ReturnString, _
  160.                 String2:=Line3)
  161.     End If
  162.     
  163.     If City > "" Then
  164.         ReturnString = _
  165.             pvtContatenateWithCRLF( _
  166.                 String1:=ReturnString, _
  167.                 String2:=City) _
  168.                 & ", "
  169.     End If
  170.     
  171. ' the conventional (i.e. GUI-centric) VB programming
  172. '   techniques must use StateCode.  The Most
  173. '   Totally Coolest way (the OO way) is to imbed the
  174. '   entire State object within the Address.  Yields
  175. '   not only the StateCode, but anything else known
  176. '   about that State
  177.     If Not pvtState Is Nothing Then
  178.         ReturnString = ReturnString & pvtState.StateCode
  179.     ElseIf StateCode > "" Then
  180.         ReturnString = ReturnString & pvtStateCode
  181.     End If
  182.     
  183.     If ZipCode >= 0 Then
  184.         ReturnString = ReturnString & " " & FormattedZip
  185.     End If
  186.     
  187. '' show-off a little by revealing interesting information
  188. ''   available only through having a rich object
  189. ''   model
  190. '    If Not pvtState Is Nothing Then
  191. '        ReturnString = _
  192. '            pvtContatenateWithCRLF( _
  193. '                String1:=ReturnString, _
  194. '                String2:="(State's Capital is " & _
  195. '                    pvtState.CapitalCity & _
  196. '                ")")
  197. '    End If
  198.     
  199.     FormattedAddress = ReturnString
  200. End Function
  201.  
  202. Public Function ObjectSortCompare(Optional SortField As Variant, Optional SortOrder As Variant, Optional CompareObject As Variant) As Long
  203. ' Support the Collection.Sort method
  204. ' Note: use the ObjectManager.ObjectSortCompare
  205. '   method for assistance
  206.  
  207.     Select Case SortField
  208.         Case "FormattedAddress"
  209.             ObjectSortCompare = _
  210.                 ObjectManager.ObjectSortCompare( _
  211.                     Value1:=FormattedAddress, _
  212.                     Value2:=CompareObject.FormattedAddress, _
  213.                     SortOrder:=SortOrder)
  214.         
  215.         Case "City"
  216.             ObjectSortCompare = _
  217.                 ObjectManager.ObjectSortCompare( _
  218.                     Value1:=City, _
  219.                     Value2:=CompareObject.City, _
  220.                     SortOrder:=SortOrder)
  221.         
  222.         Case "StateCode"
  223.             ObjectSortCompare = _
  224.                 ObjectManager.ObjectSortCompare( _
  225.                     Value1:=State.StateCode, _
  226.                     Value2:=CompareObject.State.StateCode, _
  227.                     SortOrder:=SortOrder)
  228.         
  229.         Case "StateCapitalCity"
  230.             ObjectSortCompare = _
  231.                 ObjectManager.ObjectSortCompare( _
  232.                     Value1:=State.CapitalCity, _
  233.                     Value2:=CompareObject.State.CapitalCity, _
  234.                     SortOrder:=SortOrder)
  235.         
  236.         Case "ZipCode"
  237.             ObjectSortCompare = _
  238.                 ObjectManager.ObjectSortCompare( _
  239.                     Value1:=FormattedZip, _
  240.                     Value2:=CompareObject.FormattedZip, _
  241.                     SortOrder:=SortOrder)
  242.     End Select
  243.  
  244. End Function
  245.  
  246. Private Function pvtContatenateWithCRLF(Optional String1 As Variant, Optional String2 As Variant) As String
  247.  
  248.     Dim ReturnString As String
  249.     
  250.     ReturnString = ""
  251.     
  252.     If Not IsMissing(String1) Then
  253.         ReturnString = String1
  254.         If String1 > "" Then
  255.             ReturnString = ReturnString & vbCrLf
  256.         End If
  257.     End If
  258.     
  259.     If Not IsMissing(String2) Then
  260.         If String2 > "" Then
  261.             ReturnString = ReturnString & String2
  262.         End If
  263.     End If
  264.  
  265.     pvtContatenateWithCRLF = ReturnString
  266. End Function
  267. Public Function FormattedZip() As String
  268.  
  269.     Dim ReturnString As String
  270.     
  271.     ReturnString = ""
  272.     
  273.     If ZipCode >= 0 Then
  274.         ReturnString = ReturnString & Format$(ZipCode, "00000")
  275.     End If
  276.     
  277.     If ZipSupplement > 0 Then
  278.         If ReturnString = "" Then
  279.             ReturnString = Format$(ZipSupplement, "0000")
  280.         Else
  281.             ReturnString = ReturnString & "-" & Format$(ZipSupplement, "0000")
  282.         End If
  283.     End If
  284.     
  285.     If ZipExtension > 0 Then
  286.         If ReturnString = "" Then
  287.             ReturnString = Format$(ZipExtension, "00")
  288.         Else
  289.             ReturnString = ReturnString & "-" & Format$(ZipExtension, "00")
  290.         End If
  291.     End If
  292.  
  293.     FormattedZip = ReturnString
  294. End Function
  295.  
  296.  
  297. Public Function SetStateFromStateCode(StateCode As String) As Variant
  298. ' Convert the StateCode value to a State object
  299.  
  300.     Dim NewState As New State
  301.     
  302.     Set NewState = _
  303.         ObjectManager. _
  304.             NewObject( _
  305.                 Sample:=NewState, _
  306.                 WhereClause:="StateCode = '" & StateCode & "'")
  307.                 
  308.     If Not NewState Is Nothing Then
  309.         Set State = NewState
  310.         Set SetStateFromStateCode = NewState
  311.     Else
  312.         Set SetStateFromStateCode = Nothing
  313.     End If
  314.     
  315.     Set NewState = Nothing
  316. End Function
  317.  
  318. Public Property Get State() As State
  319.     Set State = pvtState
  320. End Property
  321.  
  322.  
  323. Public Property Set State(aState As State)
  324.     Set pvtState = aState
  325. End Property
  326.  
  327. Private Sub Class_Initialize()
  328.    
  329.     Set ObjectManager = Nothing
  330.     
  331.     ZipCode = -1
  332.     ZipSupplement = -1
  333.     ZipExtension = -1
  334.     ForeignZipCode = ""
  335.     Status = "Current"
  336.     Usage = "Primary"
  337.  
  338. End Sub
  339.  
  340.  
  341.  
  342.  
  343. Public Function ObjectInitializeFromRecordSet(Optional RecordSet As Variant) As Address
  344. ' Populate my variables from the RecordSet
  345. '   (in support of VBOFCollection)
  346.  
  347.     Dim NewState As New State
  348.  
  349.     On Local Error Resume Next
  350.     
  351.     Line1 = RecordSet("Line1")
  352.     Line2 = RecordSet("Line2")
  353.     Line3 = RecordSet("Line3")
  354.     City = RecordSet("City") & ""
  355.     ZipCode = RecordSet("ZipCode") + 0
  356.     ZipSupplement = RecordSet("ZipSupplement") + 0
  357.     ZipExtension = RecordSet("ZipExtension") + 0
  358.     ForeignZipCode = RecordSet("ForeignZipCode") & ""
  359.     Status = RecordSet("Status") & ""
  360.     Usage = RecordSet("Usage") & ""
  361.     
  362.     ObjectID = RecordSet("ObjectID")
  363.     
  364. ' pick-up the State object
  365.     If Not IsNull(RecordSet("StateObjectID")) Then
  366.         Set pvtState = _
  367.             ObjectManager. _
  368.                 NewObject( _
  369.                     Sample:=NewState, _
  370.                     ObjectID:=CStr(RecordSet("StateObjectID")))
  371.     End If
  372.  
  373.     Set ObjectInitializeFromRecordSet = Me
  374. End Function
  375.  
  376. Public Function ObjectListBoxValue() As String
  377. ' Return a String will represent this object
  378. '   in a ListBox
  379. '   (in support of VBOFCollection)
  380.  
  381.     ObjectListBoxValue = _
  382.         Line1 & " " & _
  383.         City & ", " & _
  384.         StateCode & " " & _
  385.         FormattedZip
  386.  
  387. End Function
  388.  
  389.  
  390. Public Function ObjectNewInstanceOfMyClass() As Address
  391. ' Return a new instance of this class
  392. '   (in support of VBOFCollection)
  393.  
  394.     Set ObjectNewInstanceOfMyClass = New Address
  395. End Function
  396.  
  397.  
  398.  
  399. Public Function ObjectInitializeRecordSet(Optional RecordSet As Variant) As Long
  400. ' Populate the RecordSet with my variables.
  401. ' Note:  Do not initialize the ObjectID column.
  402. ' Return any error code encountered.
  403. '   (in support of VBOFCollection)
  404.  
  405.     On Local Error GoTo InitializeRecordSet_SetError
  406.     Err = 0
  407.     
  408.     RecordSet("Line1") = Line1
  409.     RecordSet("Line2") = Line2
  410.     RecordSet("Line3") = Line3
  411.     RecordSet("City") = City
  412.     RecordSet("ZipCode") = ZipCode
  413.     RecordSet("ZipSupplement") = ZipSupplement
  414.     RecordSet("ZipExtension") = ZipExtension
  415.     RecordSet("ForeignZipCode") = ForeignZipCode
  416.     RecordSet("Status") = Status
  417.     RecordSet("Usage") = Usage
  418.     
  419. ' set the State object
  420.     If Not pvtState Is Nothing Then
  421.         RecordSet("StateObjectID") = _
  422.             pvtState.ObjectID
  423.     Else
  424.         RecordSet("StateObjectID") = Null
  425.     End If
  426.     
  427. ' Note:  Do not initialize the ObjectID column.
  428.     
  429.     GoTo InitializeRecordSet_SetError
  430.  
  431. InitializeRecordSet_SetError:
  432.     ObjectInitializeRecordSet = Err
  433.     Exit Function
  434. End Function
  435.  
  436. Private Sub Class_Terminate()
  437.     If Not ObjectManager Is Nothing Then
  438.         ObjectManager.TerminateObject _
  439.             Object:=Me
  440.     End If
  441. End Sub
  442.  
  443. Public Property Get Line1() As String
  444.     Line1 = pvtLine1
  445. End Property
  446.  
  447. Public Property Let Line1(aString As String)
  448.     pvtLine1 = aString
  449. '    ObjectHasChanged
  450. End Property
  451.  
  452. Public Property Get Line2() As String
  453.     Line2 = pvtLine2
  454. End Property
  455.  
  456. Public Property Let Line2(aString As String)
  457.     pvtLine2 = aString
  458. '    ObjectHasChanged
  459. End Property
  460.  
  461. Public Property Get Line3() As String
  462.     Line3 = pvtLine3
  463. End Property
  464.  
  465. Public Property Let Line3(aString As String)
  466.     pvtLine3 = aString
  467. '    ObjectHasChanged
  468. End Property
  469.  
  470. Public Property Get City() As String
  471.     City = pvtCity
  472. End Property
  473.  
  474. Public Property Let City(aString As String)
  475.     pvtCity = aString
  476. '    ObjectHasChanged
  477. End Property
  478.  
  479. Public Property Get StateCode() As String
  480.     
  481.     If Not pvtState Is Nothing Then
  482.         StateCode = pvtState.StateCode
  483.     Else
  484.         StateCode = pvtStateCode
  485.     End If
  486. End Property
  487.  
  488.  
  489. Public Property Let StateCode(aString As String)
  490. ' StateCode is not really stored in the Address
  491. '   object (that would be too conventional.)  To
  492. '   expoit the power of OO, the Address object
  493. '   stores a reference to the entire State object,
  494. '   not just its StateCode.
  495. ' So, aString must be converted intoa State object
  496.  
  497.     SetStateFromStateCode (aString)
  498. End Property
  499.  
  500. Public Property Get ZipCode() As Long
  501.     ZipCode = pvtZipCode
  502. End Property
  503.  
  504. Public Property Let ZipCode(aLong As Long)
  505.     pvtZipCode = aLong
  506. '    ObjectHasChanged
  507. End Property
  508.  
  509. Public Property Get ZipSupplement() As Integer
  510.     ZipSupplement = pvtZipSupplement
  511. End Property
  512.  
  513. Public Property Let ZipSupplement(anInteger As Integer)
  514.     pvtZipSupplement = anInteger
  515. '    ObjectHasChanged
  516. End Property
  517.  
  518. Public Property Get ZipExtension() As Integer
  519.     ZipExtension = pvtZipExtension
  520. End Property
  521.  
  522. Public Function ZipExtensionString() As String
  523.     If pvtZipExtension >= 0 Then
  524.         ZipExtensionString = pvtZipExtension
  525.     Else
  526.         ZipExtensionString = ""
  527.     End If
  528. End Function
  529.  
  530. Public Function ZipSupplementString() As String
  531.     If pvtZipSupplement >= 0 Then
  532.         ZipSupplementString = pvtZipSupplement
  533.     Else
  534.         ZipSupplementString = ""
  535.     End If
  536. End Function
  537.  
  538.  
  539. Public Property Let ZipExtension(anInteger As Integer)
  540.     pvtZipExtension = anInteger
  541. '    ObjectHasChanged
  542. End Property
  543.  
  544. Public Property Get ForeignZipCode() As String
  545.     ForeignZipCode = pvtForeignZipCode
  546. End Property
  547.  
  548. Public Property Let ForeignZipCode(aString As String)
  549.     pvtForeignZipCode = aString
  550. '    ObjectHasChanged
  551. End Property
  552.  
  553. Public Property Get Status() As String
  554.     Status = pvtStatus
  555. End Property
  556.  
  557. Public Property Let Status(aString As String)
  558.     pvtStatus = aString
  559. '    ObjectHasChanged
  560. End Property
  561.  
  562. Public Property Get Usage() As String
  563.     Usage = pvtUsage
  564. End Property
  565.  
  566. Public Property Let Usage(aString As String)
  567.     pvtUsage = aString
  568. '    ObjectHasChanged
  569. End Property
  570.  
  571. Public Function ObjectHasChanged()
  572. ' Mark this object as "Changed" and trigger the
  573. '   "Changed" event
  574.  
  575.     On Local Error Resume Next
  576.  
  577.     ObjectChanged = True
  578.     
  579. #If NoEventMgr = False Then
  580.     If Not ObjectManager Is Nothing Then
  581.         ObjectManager. _
  582.             TriggerObjectEvent _
  583.                 Event:="Changed", _
  584.                 Object:=Me
  585.     End If
  586. #End If
  587. End Function
  588.  
  589.  
  590.  
  591. Public Function ObjectEventCallBack(Optional Event As Variant, Optional Object As Variant) As Long
  592. ' Receive the Trigger notification and process
  593. '   accordingly
  594. '
  595. ' Parameters:
  596. '   Event
  597. '       a string which identifies the Event
  598. '       Example: "Changed", "Created", "Deleted"
  599. '   Object
  600. '       the object originating the Event.
  601. '       responds to:
  602. '           TypeName(TriggerObject)
  603. '           TriggerObject.ObjectID
  604. ' (supported by VBOFEventManager)
  605.  
  606. End Function
  607.  
  608.  
  609.  
  610.